package com.schoolvisitors.controller.user;

import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.context.UserContext;
import com.schoolvisitors.entity.Reservation;
import com.schoolvisitors.entity.User;
import com.schoolvisitors.exception.BusinessException;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.response.ResultCode;
import com.schoolvisitors.service.ReservationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/v1/user")
public class ReservationController {
    private final ReservationService reservationService;

    public ReservationController(ReservationService reservationService) {
        this.reservationService = reservationService;
    }

    @PostMapping("/clockInByReservationId")
    public ResultBody clockInByReservationId(@RequestBody JSONObject json) {
        Long reservationId = json.getLong("reservationId");
        User user = UserContext.getUser();
        log.info("reservationId: {}", reservationId);
        if (reservationId == null) {
            throw new BusinessException(ResultCode.RESERVATION_NOT_FOUND);
        }
        reservationService.clockInByReservationId(user, reservationId);
        return ResultBody.success();
    }

    @PostMapping("/clockOutByReservationId")
    public ResultBody clockOutByReservationId(@RequestBody JSONObject json) {
        User user = UserContext.getUser();
        Long reservationId = json.getLong("reservationId");
        if (reservationId == null) {
            throw new BusinessException(ResultCode.RESERVATION_NOT_FOUND);
        }
        reservationService.clockOutByReservationId(user, reservationId);
        return ResultBody.success();
    }
}
